import pandas as pd
import uuid
import json
import time
import mysql.connector
import tos

# Access key 和 Secret key 可在用户火山引擎账号中查找
ak = "AKLTMmFmMzVmNmY5ZDEyNDNmNWEyZTU2MDM3Y2EzMDJlZTk"
sk = "WVdNeFpXRXdNekV4Tm1ZME5EZGxPRGc0TUdFNE1HWXdNV1ZqTkdNNU9HRQ=="
# your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例，your endpoint 填写 tos-cn-beijing.volces.com，your region 填写 cn-beijing。
endpoint = "tos-cn-beijing.ivolces.com"
region = "cn-beijing"

def acc_car_statue_label(vechicle_id,start_time_str,bagid,path,ICU2_Odometer,uuids,file_type,daystr,hourstr,IDB3_VehicleSpd,ACU2_LongAccSensorValue,ACU2_LatAccSensorValue,ACU2_VehicleDynYawRate,EPS1_SteerAngleSpd,EPS1_TorsionBarTorque,CS1_GearPositionReqSt):
    dict = {}
    dict["vehicle_id"] = vechicle_id
    dict["start_time_str"] = start_time_str
    dict["scid"] = bagid
    dict["path"] = path
    level1 = "FCW二级预警"
    level2 = "车辆状态"
    level3 = "里程:"+str(ICU2_Odometer)
    dict["level1"] = level1
    dict["level2"] = level2
    dict["level3"] = level3

    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
    dict["uid"] = uid

    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
    add_to_mysql(json_string)

    timestamp = get_timestamp(start_time_str)
    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level3
    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
    label_bucket_name = "bigdatatest"
    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
    uploadjson(label_bucket_name, labe_object_key, json_string)


    print("车速。。。。。")
    dict = {}
    dict["vehicle_id"] = vechicle_id
    dict["start_time_str"] = start_time_str
    dict["scid"] = bagid
    dict["path"] = path
    level2 = "车辆状态"
    level3 = "车速:"+str(IDB3_VehicleSpd)
    dict["level1"] = level1
    dict["level2"] = level2
    dict["level3"] = level3

    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
    dict["uid"] = uid

    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
    add_to_mysql(json_string)

    timestamp = get_timestamp(start_time_str)
    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level3
    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
    label_bucket_name = "bigdatatest"
    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
    uploadjson(label_bucket_name, labe_object_key, json_string)

    print("纵向加速度。。。。。")
    dict = {}
    dict["vehicle_id"] = vechicle_id
    dict["start_time_str"] = start_time_str
    dict["scid"] = bagid
    dict["path"] = path
    level2 = "车辆状态"
    level3 = "纵向加速度:"+str(ACU2_LongAccSensorValue)
    dict["level1"] = level1
    dict["level2"] = level2
    dict["level3"] = level3

    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
    dict["uid"] = uid

    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
    add_to_mysql(json_string)

    timestamp = get_timestamp(start_time_str)
    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level3
    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
    label_bucket_name = "bigdatatest"
    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
    uploadjson(label_bucket_name, labe_object_key, json_string)

    print("横向加速度。。。。。")
    dict = {}
    dict["vehicle_id"] = vechicle_id
    dict["start_time_str"] = start_time_str
    dict["scid"] = bagid
    dict["path"] = path
    level2 = "车辆状态"
    level3 = "横向加速度:"+str(ACU2_LatAccSensorValue)
    dict["level1"] = level1
    dict["level2"] = level2
    dict["level3"] = level3

    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
    dict["uid"] = uid

    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
    add_to_mysql(json_string)

    timestamp = get_timestamp(start_time_str)
    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level3
    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
    label_bucket_name = "bigdatatest"
    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
    uploadjson(label_bucket_name, labe_object_key, json_string)

    print("横摆角速率。。。。。")
    dict = {}
    dict["vehicle_id"] = vechicle_id
    dict["start_time_str"] = start_time_str
    dict["scid"] = bagid
    dict["path"] = path
    level2 = "车辆状态"
    level3 = "横摆角速率:"+str(ACU2_VehicleDynYawRate)
    dict["level1"] = level1
    dict["level2"] = level2
    dict["level3"] = level3

    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
    dict["uid"] = uid

    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
    add_to_mysql(json_string)

    timestamp = get_timestamp(start_time_str)
    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level3
    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
    label_bucket_name = "bigdatatest"
    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
    uploadjson(label_bucket_name, labe_object_key, json_string)


    print("方向盘转角速率。。。。。")
    dict = {}
    dict["vehicle_id"] = vechicle_id
    dict["start_time_str"] = start_time_str
    dict["scid"] = bagid
    dict["path"] = path
    level2 = "车辆状态"
    level3 = "方向盘转角速率:"+str(EPS1_SteerAngleSpd)
    dict["level1"] = level1
    dict["level2"] = level2
    dict["level3"] = level3

    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
    dict["uid"] = uid

    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
    add_to_mysql(json_string)

    timestamp = get_timestamp(start_time_str)
    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level3
    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
    label_bucket_name = "bigdatatest"
    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
    uploadjson(label_bucket_name, labe_object_key, json_string)


    print("方向盘手力矩。。。。。")
    dict = {}
    dict["vehicle_id"] = vechicle_id
    dict["start_time_str"] = start_time_str
    dict["scid"] = bagid
    dict["path"] = path
    level2 = "车辆状态"
    level3 = "方向盘手力矩:"+str(EPS1_TorsionBarTorque)
    dict["level1"] = level1
    dict["level2"] = level2
    dict["level3"] = level3

    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
    dict["uid"] = uid

    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
    add_to_mysql(json_string)

    timestamp = get_timestamp(start_time_str)
    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level3
    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
    label_bucket_name = "bigdatatest"
    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
    uploadjson(label_bucket_name, labe_object_key, json_string)



    print("档位。。。。。")
    dict = {}
    dict["vehicle_id"] = vechicle_id
    dict["start_time_str"] = start_time_str
    dict["scid"] = bagid
    dict["path"] = path
    level2 = "车辆状态"
    level3 = "档位:"+str(CS1_GearPositionReqSt)
    dict["level1"] = level1
    dict["level2"] = level2
    dict["level3"] = level3

    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
    dict["uid"] = uid

    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
    add_to_mysql(json_string)

    timestamp = get_timestamp(start_time_str)
    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level3
    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
    label_bucket_name = "bigdatatest"
    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
    uploadjson(label_bucket_name, labe_object_key, json_string)
def uploadjson(bucket_name,object_key,jsonstr):
    try:
        client = tos.TosClientV2(ak, sk, endpoint, region)
        # 若在上传对象时设置文件存储类型（x-tos-storage-class）和访问权限 (x-tos-acl), 请在 put_object中设置相关参数
        # 用户在上传对象时，可以自定义元数据，以便对对象进行自定义管理
        # result = client.put_object(bucket_name, object_key, content=content, acl=tos.ACLType.ACL_Private, storage_class=tos.StorageClassType.Storage_Class_Standard, meta={'name': '张三', 'age': '20'})
        result = client.put_object(bucket_name, object_key, content=jsonstr)
        # HTTP状态码
        print('http status code:{}'.format(result.status_code))
        # 请求ID。请求ID是本次请求的唯一标识，建议在日志中添加此参数
        print('request_id: {}'.format(result.request_id))
        # hash_crc64_ecma 表示该对象的64位CRC值, 可用于验证上传对象的完整性
        print('crc64: {}'.format(result.hash_crc64_ecma))
    except tos.exceptions.TosClientError as e:
        # 操作失败，捕获客户端异常，一般情况为非法请求参数或网络异常
        print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
    except tos.exceptions.TosServerError as e:
        # 操作失败，捕获服务端异常，可从返回信息中获取详细错误信息
        print('fail with server error, code: {}'.format(e.code))
        # request id 可定位具体问题，强烈建议日志中保存
        print('error with request id: {}'.format(e.request_id))
        print('error with message: {}'.format(e.message))
        print('error with http code: {}'.format(e.status_code))
        print('error with ec: {}'.format(e.ec))
        print('error with request url: {}'.format(e.request_url))
    except Exception as e:
        print('fail with unknown error: {}'.format(e))
def add_to_mysql(json_str):
    # 配置数据库连接信息
    db_config = {
        'host': 'mysql-d8beb8b0655b-public.rds.volces.com',
        'user': 'user01',
        'password': 'password123!#',
        'database': 'bi_test'
    }
    # 创建数据库连接
    conn = mysql.connector.connect(**db_config)
    cursor = conn.cursor()

    # 假设你有一个名为 'your_table' 的表，包含 'column1', 'column2', 'column3' 等字段
    table_name = 'trigger_sc_ep40_tda4'

    # 假设你有一个数据字典，包含要插入的数据
    data_to_insert = json.loads(json_str)

    # 构建 SQL 插入语句
    columns = ', '.join(data_to_insert.keys())
    values = ', '.join(['%s'] * len(data_to_insert))
    sql_insert = f"replace into {table_name} ({columns}) VALUES ({values})"

    # 执行插入操作
    cursor.execute(sql_insert, list(data_to_insert.values()))

    # 提交更改
    conn.commit()
def get_timestamp(time_string):
    # 转换为时间元组
    time_tuple = time.strptime(time_string, "%Y-%m-%d %H:%M:%S")
    # 转换为时间戳
    timestamp = str(int(time.mktime(time_tuple)*1000))
    return timestamp
def get_fcw_level2_can_100ms(df_can_100ms_save_path, vechicle_id, daystr, hourstr, bagid, uuids, file_type):
    try:
        df_can_100ms_pilot_active = pd.read_pickle(df_can_100ms_save_path)
    except Exception as e:
        print('data report read error, ', str(e))

    df_can_100ms_pilot_active_list = df_can_100ms_pilot_active[
        ['start_time_str', 'path','ICU2_Odometer', 'IDB3_VehicleSpd', 'ACU2_LongAccSensorValue', 'ACU2_LatAccSensorValue',
         'ACU2_VehicleDynYawRate','EPS1_SteerAngleSpd', 'EPS1_TorsionBarTorque', 'CS1_GearPositionReqSt',
         'ADCS8_FCWStatus','IDB1_BrakePedalApplied','VLCCDHypotheses_Hypothesis_0_fVrely',
         'VLCCDHypotheses_Hypothesis_0_fVrelX','VLCCDHypotheses_Hypothesis_0_eEBAObjectClass']].values.tolist()

    found_index = -9999
    # 遍历列表
    for index, element in enumerate(df_can_100ms_pilot_active_list):
        if element[10] != 1 or element[10] != 2:
            found_index = index
            break  # 找到第一个符合条件的元素就跳出循环

    if found_index==-9999:
        start_time_str=df_can_100ms_pilot_active_list[0][0]
        path = df_can_100ms_pilot_active_list[0][1]
        ICU2_Odometer = df_can_100ms_pilot_active_list[0][2]
        IDB3_VehicleSpd = df_can_100ms_pilot_active_list[0][3]
        ACU2_LongAccSensorValue = df_can_100ms_pilot_active_list[0][4]
        ACU2_LatAccSensorValue = df_can_100ms_pilot_active_list[0][5]
        ACU2_VehicleDynYawRate = df_can_100ms_pilot_active_list[0][6]
        EPS1_SteerAngleSpd = df_can_100ms_pilot_active_list[0][7]
        EPS1_TorsionBarTorque = df_can_100ms_pilot_active_list[0][8]
        CS1_GearPositionReqSt = df_can_100ms_pilot_active_list[0][9]
        ADCS8_FCWStatus = df_can_100ms_pilot_active_list[0][10]
        dict = {}
        dict["vehicle_id"] = vechicle_id
        dict["start_time_str"] = start_time_str
        dict["scid"] = bagid
        dict["path"] = path
        level1 = "FCW二级预警"
        level2 = "未触发FCW"
        level3 = ""
        dict["level1"] = level1
        dict["level2"] = level2
        dict["level3"] = level3

        print("start_time_str_100: " + start_time_str)
        uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
        uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
        dict["uid"] = uid

        json_string = json.dumps(dict, ensure_ascii=False, indent=None)
        add_to_mysql(json_string)

        print("start_time_str_100: " + start_time_str)
        timestamp = get_timestamp(start_time_str)
        input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
        filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
        label_bucket_name = "bigdatatest"
        labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
        uploadjson(label_bucket_name, labe_object_key, json_string)

    else:
        if found_index>=3:
            start_time_str = df_can_100ms_pilot_active_list[found_index][0]
            path = df_can_100ms_pilot_active_list[found_index][1]
            ICU2_Odometer = df_can_100ms_pilot_active_list[found_index][2]
            IDB3_VehicleSpd = df_can_100ms_pilot_active_list[found_index][3]
            ACU2_LongAccSensorValue = df_can_100ms_pilot_active_list[found_index][4]
            ACU2_LatAccSensorValue = df_can_100ms_pilot_active_list[found_index][5]
            ACU2_VehicleDynYawRate = df_can_100ms_pilot_active_list[found_index][6]
            EPS1_SteerAngleSpd = df_can_100ms_pilot_active_list[found_index][7]
            EPS1_TorsionBarTorque = df_can_100ms_pilot_active_list[found_index][8]
            CS1_GearPositionReqSt = df_can_100ms_pilot_active_list[found_index][9]
            ADCS8_FCWStatus = df_can_100ms_pilot_active_list[found_index][10]
            IDB1_BrakePedalApplied_last1=df_can_100ms_pilot_active_list[found_index-1][11]
            IDB1_BrakePedalApplied_last2 = df_can_100ms_pilot_active_list[found_index-2][11]
            IDB1_BrakePedalApplied_last3 = df_can_100ms_pilot_active_list[found_index-3][11]
            EPS1_SteerAngleSpd_last1= df_can_100ms_pilot_active_list[found_index-1][7]
            EPS1_SteerAngleSpd_last2= df_can_100ms_pilot_active_list[found_index -2][7]
            EPS1_SteerAngleSpd_last3 = df_can_100ms_pilot_active_list[found_index - 3][7]
            EPS1_SteerAngleSpd_next1= df_can_100ms_pilot_active_list[found_index+1][7]
            EPS1_SteerAngleSpd_next2= df_can_100ms_pilot_active_list[found_index + 2][7]
            EPS1_SteerAngleSpd_next3= df_can_100ms_pilot_active_list[found_index + 3][7]

            VLCCDHypotheses_Hypothesis_0_fVrely=df_can_100ms_pilot_active_list[found_index][12]
            VLCCDHypotheses_Hypothesis_0_fVrelX=df_can_100ms_pilot_active_list[found_index][13]
            VLCCDHypotheses_Hypothesis_0_eEBAObjectClass=df_can_100ms_pilot_active_list[found_index][14]


            if ADCS8_FCWStatus==2 and ((IDB1_BrakePedalApplied_last1==1 and IDB1_BrakePedalApplied_last2==1 and IDB1_BrakePedalApplied_last3==1)
            or (abs(EPS1_SteerAngleSpd_last1)>=60 or abs(EPS1_SteerAngleSpd_last2)>=60 or abs(EPS1_SteerAngleSpd_last3)>=60 or abs(EPS1_SteerAngleSpd_next1)>=60 or abs(EPS1_SteerAngleSpd_next2)>=60 or abs(EPS1_SteerAngleSpd_next3)>=60)):
                dict = {}
                dict["vehicle_id"] = vechicle_id
                dict["start_time_str"] = start_time_str
                dict["scid"] = bagid
                dict["path"] = path
                level1 = "FCW二级预警"
                level2 = "正触发"
                level3 = ""
                dict["level1"] = level1
                dict["level2"] = level2
                dict["level3"] = level3

                print("start_time_str_100: " + start_time_str)
                uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                dict["uid"] = uid

                json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                add_to_mysql(json_string)

                print("start_time_str_100: " + start_time_str)
                timestamp = get_timestamp(start_time_str)
                input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                label_bucket_name = "bigdatatest"
                labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                uploadjson(label_bucket_name, labe_object_key, json_string)

            else:
                if ADCS8_FCWStatus==1:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "一级预警"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)

                elif ADCS8_FCWStatus==2:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "二级预警"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)


                if abs(VLCCDHypotheses_Hypothesis_0_fVrely)>1 and abs( IDB3_VehicleSpd/3.6 + VLCCDHypotheses_Hypothesis_0_fVrelX)<=1:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "横穿"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)

                elif abs(VLCCDHypotheses_Hypothesis_0_fVrely)>1 and abs( IDB3_VehicleSpd/3.6 + VLCCDHypotheses_Hypothesis_0_fVrelX)>1:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "斜穿"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)

                elif (IDB3_VehicleSpd/3.6 + VLCCDHypotheses_Hypothesis_0_fVrelX)> -2:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "纵向OnComing"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)
                else:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "纵向跟随"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)

                if VLCCDHypotheses_Hypothesis_0_eEBAObjectClass==1:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "行人"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)
                elif VLCCDHypotheses_Hypothesis_0_eEBAObjectClass==2:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "车辆"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)
                elif VLCCDHypotheses_Hypothesis_0_eEBAObjectClass == 3:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "两轮车"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)
        elif found_index<3:
            start_time_str = df_can_100ms_pilot_active_list[found_index][0]
            path = df_can_100ms_pilot_active_list[found_index][1]
            ICU2_Odometer = df_can_100ms_pilot_active_list[found_index][2]
            IDB3_VehicleSpd = df_can_100ms_pilot_active_list[found_index][3]
            ACU2_LongAccSensorValue = df_can_100ms_pilot_active_list[found_index][4]
            ACU2_LatAccSensorValue = df_can_100ms_pilot_active_list[found_index][5]
            ACU2_VehicleDynYawRate = df_can_100ms_pilot_active_list[found_index][6]
            EPS1_SteerAngleSpd = df_can_100ms_pilot_active_list[found_index][7]
            EPS1_TorsionBarTorque = df_can_100ms_pilot_active_list[found_index][8]
            CS1_GearPositionReqSt = df_can_100ms_pilot_active_list[found_index][9]
            ADCS8_FCWStatus = df_can_100ms_pilot_active_list[found_index][10]

            EPS1_SteerAngleSpd_next1= df_can_100ms_pilot_active_list[found_index+1][7]
            EPS1_SteerAngleSpd_next2= df_can_100ms_pilot_active_list[found_index + 2][7]
            EPS1_SteerAngleSpd_next3= df_can_100ms_pilot_active_list[found_index + 3][7]

            VLCCDHypotheses_Hypothesis_0_fVrely=df_can_100ms_pilot_active_list[found_index][12]
            VLCCDHypotheses_Hypothesis_0_fVrelX=df_can_100ms_pilot_active_list[found_index][13]
            VLCCDHypotheses_Hypothesis_0_eEBAObjectClass=df_can_100ms_pilot_active_list[found_index][14]

            if ADCS8_FCWStatus==2 and ((abs(EPS1_SteerAngleSpd_next1)>=60 or abs(EPS1_SteerAngleSpd_next2)>=60 or abs(EPS1_SteerAngleSpd_next3)>=60)):
                dict = {}
                dict["vehicle_id"] = vechicle_id
                dict["start_time_str"] = start_time_str
                dict["scid"] = bagid
                dict["path"] = path
                level1 = "FCW二级预警"
                level2 = "正触发"
                level3 = ""
                dict["level1"] = level1
                dict["level2"] = level2
                dict["level3"] = level3

                print("start_time_str_100: " + start_time_str)
                uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                dict["uid"] = uid

                json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                add_to_mysql(json_string)

                print("start_time_str_100: " + start_time_str)
                timestamp = get_timestamp(start_time_str)
                input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                label_bucket_name = "bigdatatest"
                labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                uploadjson(label_bucket_name, labe_object_key, json_string)

            else:
                if ADCS8_FCWStatus==1:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "一级预警"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)

                elif ADCS8_FCWStatus==2:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "二级预警"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)


                if abs(VLCCDHypotheses_Hypothesis_0_fVrely)>1 and abs( IDB3_VehicleSpd/3.6 + VLCCDHypotheses_Hypothesis_0_fVrelX)<=1:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "横穿"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)

                elif abs(VLCCDHypotheses_Hypothesis_0_fVrely)>1 and abs( IDB3_VehicleSpd/3.6 + VLCCDHypotheses_Hypothesis_0_fVrelX)>1:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "斜穿"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)

                elif (IDB3_VehicleSpd/3.6 + VLCCDHypotheses_Hypothesis_0_fVrelX)> -2:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "纵向OnComing"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)
                else:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "纵向跟随"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)

                if VLCCDHypotheses_Hypothesis_0_eEBAObjectClass==1:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "行人"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)
                elif VLCCDHypotheses_Hypothesis_0_eEBAObjectClass==2:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "车辆"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)
                elif VLCCDHypotheses_Hypothesis_0_eEBAObjectClass == 3:
                    dict = {}
                    dict["vehicle_id"] = vechicle_id
                    dict["start_time_str"] = start_time_str
                    dict["scid"] = bagid
                    dict["path"] = path
                    level1 = "FCW二级预警"
                    level2 = "误触发"
                    level3 = "两轮车"
                    dict["level1"] = level1
                    dict["level2"] = level2
                    dict["level3"] = level3

                    print("start_time_str_100: " + start_time_str)
                    uid_string = vechicle_id + start_time_str + bagid + level1 + level2 + level3
                    uid = str(uuid.uuid3(uuid.NAMESPACE_DNS, uid_string))
                    dict["uid"] = uid

                    json_string = json.dumps(dict, ensure_ascii=False, indent=None)
                    add_to_mysql(json_string)

                    print("start_time_str_100: " + start_time_str)
                    timestamp = get_timestamp(start_time_str)
                    input_string = vechicle_id + bagid + timestamp + uuids + file_type + level2 + level3
                    filename = str(uuid.uuid3(uuid.NAMESPACE_DNS, input_string)) + ".json"
                    label_bucket_name = "bigdatatest"
                    labe_object_key = "EP40/TDA4/LABEL/" + daystr + "/" + hourstr + "/" + vechicle_id + "/" + filename
                    uploadjson(label_bucket_name, labe_object_key, json_string)





